<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use App\Http\Controllers\Controller;

class SettingController extends Controller
{

    //参数设置
    public function setlist(Request $request)
    {
        $id=session('user_id');
        $message=\DB::table('config')->first();
        return view('admin.setting.setlist',compact('message'));
    }
    public function updateone(Request $request){
        $data=$request->all();
        if (!preg_match("/^[0-9]+(.[0-9]{1,2})?$/",$data['ag_income1'])) {
            $array = array('msg'=>'省级代理佣金比必须是数字,且最多2位小数!','status'=>'false');
            return json_encode($array);
        }elseif (!preg_match("/^[0-9]+(.[0-9]{1,2})?$/",$data['ag_income2'])) {
            $array = array('msg'=>'市级代理佣金比必须是数字,且最多2位小数!','status'=>'false');
            return json_encode($array);
        }elseif (!preg_match("/^[0-9]+(.[0-9]{1,2})?$/",$data['fee'])) {
            $array = array('msg'=>'平台手续费比例必须是数字,且最多2位小数!','status'=>'false');
            return json_encode($array);
        }elseif (!is_numeric($data['account'])) {
            $array = array('msg'=>'平台收款账号必须是数字或数字字符串!','status'=>'false');
            return json_encode($array);
        }else{
            $res=\DB::table('config')->update(['ag_income1'=>$data['ag_income1'],'ag_income2'=>$data['ag_income2'],'fee'=>$data['fee'],'account'=>$data['account']]);
            if ($res) {
                $array = array('msg'=>'保存成功!','status'=>'true');
                return json_encode($array);
            }else{
                $array = array('msg'=>'保存失败!','status'=>'false');
                return json_encode($array);
            }
        }
    }
    //账户设置
    public function account(Request $request){
        $userid=session('user_id');
        $userinfo=\DB::table('admin')->where(['id'=>$userid])->first();
        return view('admin.setting.account',compact('userinfo'));
    }
    public function accountchange(Request $request){
        $userid=session('user_id');
        $data=$request->all();
        if ($data['newpassword']!=$data['repassword']) {
            $array = array('msg'=>'二次密码输入不一致!','status'=>'false');
            return json_encode($array);
        }
        $result=\DB::table('admin')->where(['id'=>$userid,'name'=>$data['accountname']])->first();
        if (!Hash::check($data['oldpassword'],$result->password)) {
            $array = array('msg'=>'原始密码填写错误!','status'=>'false');
            return json_encode($array);
        }
        $data['password']= Hash::make($data['newpassword']);
        $res=\DB::table('admin')->where(['id'=>$userid,'name'=>$data['accountname']])->update(['password'=>$data['password']]);
        if ($res) {
            $array = array('msg'=>'密码修改成功!','status'=>'true');
            return json_encode($array);
        }else{
            $array = array('msg'=>'密码修改失败!','status'=>'false');
            return json_encode($array);
        }
    }
}